package sanrenxing.dayEnd.backRepertoryTotal.controller;


import org.springframework.web.bind.annotation.*;
import sanrenxing.dayEnd.backRepertoryTotal.domain.Fun;
import sanrenxing.dayEnd.backRepertoryTotal.domain.Rf;
import sanrenxing.dayEnd.backRepertoryTotal.domain.Role;
import sanrenxing.dayEnd.backRepertoryTotal.domain.users;
import sanrenxing.dayEnd.backRepertoryTotal.service.FunBiz;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权限管理
 */
@RestController
@RequestMapping("/fc")
public class FunController {
    @Resource
    private FunBiz funIml;

    /**
     * 得到所有用权限
     * @param request
     * @return
     */
    @GetMapping("/sj")
    public List<Fun> sy(HttpServletRequest request){
        HttpSession hs=request.getSession();
        users us= (users) hs.getAttribute("sn");
        List<Fun> lt=funIml.all(us);
        return lt;
    }

    /**
     * 设置侧边栏
     * @param request
     * @return
     */
    @GetMapping("/sjss")
    public Map syss(HttpServletRequest request){
         Map ma=new HashMap();
        Map mas=new HashMap();
        mas.put("title","首页");
        mas.put("href","/html/zx.html");
        Map mass=new HashMap();
        mass.put("image","/images/log2.png");
        List<Map> masss=new ArrayList<>();
        HttpSession hs=request.getSession();
        users us= (users) hs.getAttribute("sn");
        List<Fun> lt=funIml.all(us);
        for (Fun fun : lt) {
            if(fun.getFunparentId().equals("root_menu")){
                Map mj=new HashMap();
                List<Map> nn=new ArrayList<>();
                mj.put("title",fun.getFunName());
                mj.put("icon",fun.getIcon());
                mj.put( "href",fun.getFunUrl());
                mj.put("target","_self");
                for (Fun funs : lt) {
                    if(fun.getFunId().equals(funs.getFunparentId())){
                        Map mjs=new HashMap();
                        List<Map> nns=new ArrayList<>();
                        mjs.put("title",funs.getFunName());
                        mjs.put("icon",funs.getIcon());
                        mjs.put("target","_self");
                        mjs.put( "href",funs.getFunUrl());
                        for (Fun funss : lt) {
                            if(funs.getFunId().equals(funss.getFunparentId())){
                                Map mjss=new HashMap();
                                mjss.put("title",funss.getFunName());
                                mjss.put("icon",funss.getIcon());
                                mjss.put("target","_self");
                                mjss.put( "href",funss.getFunUrl());
                                nns.add(mjss);
                            }
                        }
                        mjs.put("child",nns);
                        nn.add(mjs);
                    }
                }
                mj.put("child",nn);
                masss.add(mj);
            }
        }
        ma.put("homeInfo",mas);
        ma.put("logoInfo",mass);
        ma.put("menuInfo",masss);
        return ma;
    }
    /**
     * 显示页面
     * @param limit 行
     * @param page 页
     * @return
     */
    @GetMapping("/sy")
    public Map us(int limit, int page) {
    Map<String,Object> ma=new HashMap<>();
    ma.put("code",0);
        ma.put("msg","");
        ma.put("count",funIml.count());
        ma.put("data",funIml.rall(limit, page));
    return ma;
    }

    /**增加权限
     * @param us
     * @return
     */
    @PostMapping("/sy")
    public String add(@RequestBody Role us){
        return funIml.add(us)>0?"cg":"sb";
    }

    /**
     *修改权限
     * @param us
     * @return
     */
    @PutMapping("/sy")
    public String xg(@RequestBody Role us){
        return funIml.update(us)>0?"cg":"sb";
    }

    /**
     *删除权限
     * @param us
     * @return
     */
    @DeleteMapping("/sy/{id}")
    public String xg(@PathVariable("id") int us){
        return funIml.del(us)>0?"cg":"sb";
    }

    /**
     *批量删
     * @param ss
     * @return
     */
    @PutMapping("/sys/{ss}")
    public String dels(@PathVariable("ss") String ss){
        return funIml.updates(ss)>0?"cg":"sb";
    }

    /**
     *存权限关系
     * @param ss
     * @param request
     * @return
     */
    @PutMapping("/qx/{ss}")
    public String qxs(@PathVariable("ss") int ss, HttpServletRequest request){
        HttpSession hs=request.getSession();
        hs.setAttribute("qxr",ss);
        List<Rf> lt=funIml.rfal(ss);
        hs.setAttribute("qxsj",lt);
        return "";
    }

    /**
     *返回权限关系
     * @param request
     * @return
     */
    @GetMapping("/qx")
    public List<Rf> qxss(HttpServletRequest request){
        HttpSession hs=request.getSession();
        List<Rf> lt= (List<Rf>) hs.getAttribute("qxsj");
        return lt;
    }

    /**
     *设置权限
     * @param ss
     * @param request
     * @return
     */
    @PutMapping("/rqx/{ss}")
    public String rqxs(@PathVariable("ss") String ss, HttpServletRequest request){
        HttpSession hs=request.getSession();
        int sj= (int) hs.getAttribute("qxr");
        return funIml.rss(ss,sj)>0?"cg":"sb";
    }
}
